package com.ljf.framework.util;

import com.ljf.framework.exception.LjfException;

import java.util.HashMap;

/**
 * 说明：
 *
 * @Auther: lijinfeng
 * @Date: 2023/9/19
 */
public class LjfStringUtil {

    /**
     * 指定元素是否为null或者空字符串
     * @param str 指定元素
     * @return 是否为null或者空字符串
     */
    public static boolean isEmpty(String str) {
        return str == null || "".equals(str);
    }

    /**
     * 指定元素是否不为 (null或者空字符串)
     * @param str 指定元素
     * @return 是否为null或者空字符串
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 将json串转化为map
     *
     * @param str
     * @return
     */
    public static HashMap jsonStringToMap(String str) {
        HashMap<String, String> map = new HashMap<>();
        try {
            String replace = str.replace("{", "").replace("}","");
            String[] kvs = replace.split(",");
            for (String kv : kvs){
                String[] split = kv.split(":");
                map.put(split[0].replace("\"","").trim(),split[1].replace("\"","").trim());
            }
        }catch (Exception e){
            throw new LjfException("非JSON格式");
        }
        return map;
    }

    public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix) {
        if (str != null && prefix != null) {
            int preLen = prefix.length();
            return preLen > str.length() ? false : regionMatches(str, true, 0, prefix, 0, preLen);
        } else {
            return str == prefix;
        }
    }

    private static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length) {
        if (cs instanceof String && substring instanceof String) {
            return ((String)cs).regionMatches(ignoreCase, thisStart, (String)substring, start, length);
        } else {
            int index1 = thisStart;
            int index2 = start;
            int tmpLen = length;
            int srcLen = cs.length() - thisStart;
            int otherLen = substring.length() - start;
            if (thisStart >= 0 && start >= 0 && length >= 0) {
                if (srcLen >= length && otherLen >= length) {
                    while(tmpLen-- > 0) {
                        char c1 = cs.charAt(index1++);
                        char c2 = substring.charAt(index2++);
                        if (c1 != c2) {
                            if (!ignoreCase) {
                                return false;
                            }

                            char u1 = Character.toUpperCase(c1);
                            char u2 = Character.toUpperCase(c2);
                            if (u1 != u2 && Character.toLowerCase(u1) != Character.toLowerCase(u2)) {
                                return false;
                            }
                        }
                    }

                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
}
